var NonstandardHours = function() {
	return {
		datatable : null,
		init : function() {
			$("#startTime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d"});
			$("#endTime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d"});
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var dayReportListTable = $("#dayReportListTable").dataTable({
				"sAjaxSource" : ctx + "/nonstandardHours/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {page : 1,start : 0,rows : 10,params : {}};
					if($('#userIds').val()){
						postData.params.userIds = $('#userIds').val();
						console.log($('#userIds').val());					
					}
					
					if($('#startTime').val()){
						postData.params.startTime = $('#startTime').val();
					}
					
					if($('#endTime').val()){
						postData.params.endTime = $('#endTime').val();
					}
					
					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					$.ajax({
						"dataType" : 'json',
						"cache": false,
						"type" : "POST",
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
				{
					"data" : "index",
					"aTargets" : [ 0 ]
				},{
					"data" : "emplName",
					"aTargets" : [ 1 ],
					'bSortable': true
				},{
					"data" : "emplCode",
					"aTargets" : [ 2 ]
				},{
					"data" : "year",
					"aTargets" : [ 3 ],
					'bSortable': true
				},{
					"data" : "manHoursTypeName",
					"aTargets" : [ 4 ]
				},{
					"data" : "normalHours",
					"aTargets" : [ 5 ]
				},{
					"aTargets" : [ 6 ],
					"mRender" : function(data, type, full) {
						return new Date(full.billDate).format("yyyy-MM-dd");
					}
				},{
					"aTargets" : [ 7 ],
					"mRender" : function(data, type, full) {
						return '<button title="'+messages.public_button_edit+'" class="btn btn-sm-table btn-primary m-r-5" name="edit" target_id="'+full.id+'">编辑</button>';
					}
				}]
			});
			this.datatable = dayReportListTable;
		},reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#editForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener:function(){
			/**
			 * 监听开关事件
			 */
        	/*
        	 * 选择员工modal
        	 */
        	jQuery("#selected").click(function(){
        		$("#groupUserModal").modal('show');
        	});
        	/*
        	 * 查询
        	 */
        	jQuery("#searchBtn").click(function() { 
        		NonstandardHours.reSearch();
				return false;
			});
        	/*
        	 * 自定义validate验证输入的数字小数点位数不能大于一位
        	 */
            jQuery.validator.addMethod("minNumber",function(value, element){
                var returnVal = true;
                inputZ=value;
                var ArrMen= inputZ.split("."); //截取字符串
                if(ArrMen.length==2){
                    if(ArrMen[1].length>1){ //判断小数点后面的字符串长度
                        returnVal = false;
                    }
                }
                return returnVal;
            },"小数点后最多为一位"); //验证错误信息
        	/*
        	 * 保存修改后工时
        	 */
			$("#editFormBtn").click(function(){
				var editForm = $('#editForm');
				editForm.validate({
					rules : {
						normalHours : {
							required : true,
							number: true,     //输入必须是数字
							range: [0,24],
				            maxlength : 3,
				            minNumber: $("#normalHours").val() //调用自定义验证
						}
					}
				});
      			if(editForm.valid()){
      				var postData = $("form[name='editForm']").serializeJson();
      				$.ajax({
      					"dataType" : 'json',
      					"cache": false,
      					"type" : "POST",
      					"data" : postData,
      					"url" : ctx + "/nonstandardHours/edit?now="+new Date().getTime(),
      					"success" : function(data) {
      						if(data.type=='success'){
      							$("#editDlg").modal("hide");
      							BootstrapDialog.show({
      								title : messages.public_cue_cue,
      								message : messages.public_cue_saveOk,
      								buttons : [ {
      									label : messages.public_cue_sure,
      									cssClass : 'btn btn-sm btn-primary',
      									action : function(dialogItself) {
      										NonstandardHours.reSearch();
      										dialogItself.close();
      									}
      								} ]
      							});
      						}else{
      							BootstrapDialog.show({
      								type : BootstrapDialog.TYPE_WARNING,
      								title : messages.public_cue_warning,
      								message : data.message,
      								buttons : [ {
      									label : messages.public_cue_sure,
      									cssClass : 'btn btn-sm btn-primary',
      									action : function(dialogItself) {
      										NonstandardHours.reSearch();
      										dialogItself.close();
      									}
      								} ]
      							});
      						}
      					}
      				});
      			}
      		});
			
        	/*
			 * 编辑
			 */
        	jQuery("#dayReportListTable tbody").on("click", "button[name='edit']", function() {
				var id = $(this).attr("target_id");
				var form=$("form[name='editForm']");
				NonstandardHours.removeClass();
				$.ajax({
					"dataType" : 'json',
					"cache" : false,
					"type" : "POST",
					"data" : {},
					"url" : ctx + '/nonstandardHours/get/'+id+"?now="+new Date().getTime(),
					"success" : function(data, textStatus, jqXHR){
						if (data.type == 'success') {
							$(form).binddata(data.data);
							$("div[name=editDlg] h4.modal-title").html("修改工时");
							$("#editDlg").modal("show");
						} else {
							BootstrapDialog.show({
								title : messages.public_cue_message,
								message : data.message,
								buttons : [ {
									label : messages.public_cue_sure,
									action : function(dialog) {
										dialog.close();
									}
								} ]
							});
						}
					}
				});
			});
     	}
	}
}();